package com.ubhave.powertracker;

import java.util.Calendar;
import java.util.Date;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.wifi.WifiManager;
import android.os.BatteryManager;
import android.preference.PreferenceManager;
import android.util.Log;
import android.widget.Toast;

public class PowerStateChangeBroadcastReciever extends BroadcastReceiver{

	SharedPreferences settings;
	
	@Override
	public void onReceive(Context context, Intent intent) {
				
		String type="unknown";
		
		if(intent.getAction().equals("android.intent.action.ACTION_SHUTDOWN"))
			type="Shutdown";
		else if(intent.getAction().equals("android.intent.action.ACTION_BOOT_COMPLETED"))
			type="Boot";
		else if(intent.getAction().equals("android.intent.action.BOOT_COMPLETED"))
			type="Boot";
		
		Log.i("POWeR Tracker Sensor: ", "Power State Event "+type);
		
		//Toast.makeText(PowerTrackerApplication.getContext(), "POWeR Tracker Power State. "+type, Toast.LENGTH_SHORT).show();
		
		
		
		settings=PreferenceManager.getDefaultSharedPreferences(PowerTrackerApplication.getContext());
		
		if(settings.getBoolean("SensorsOn", false)){
			SharedPreferences.Editor editor = settings.edit();
			editor.putString("sensorLog", settings.getString("sensorLog", "")+"Push Sensor: PowerStateChanged("+type+") "+"TimeStamp("+new Date()+"), ");
        	editor.commit();
		}
		
		//If the phone is booting also reset the alarms
		if(type.equals("Boot")){
			
			AlarmManager alarmManager = (AlarmManager)PowerTrackerApplication.getContext().getSystemService(PowerTrackerApplication.getContext().ALARM_SERVICE);
			
			int viewAlarmHour = settings.getInt("viewNotificationTime.hour", -1);
			int viewAlarmMinute = settings.getInt("viewNotificationTime.minute", -1);
			
			int updateAlarmHour = settings.getInt("updateNotificationTime.hour", -1);
			int updateAlarmMinute = settings.getInt("updateNotificationTime.minute", -1);
			
			int questionnaireAlarmHour = settings.getInt("questionnaireNotificationTime.hour", -1);
			int questionnaireAlarmMinute = settings.getInt("questionnaireNotificationTime.minute", -1);
			
			if(viewAlarmHour!=-1&&viewAlarmMinute!=-1){			
						
				Intent myIntent = new Intent(PowerTrackerApplication.getContext() , DailyViewReminderNotification.class);     
			
		    	PendingIntent pendingIntent = PendingIntent.getService(PowerTrackerApplication.getContext(), 0, myIntent, 0);

		    	Calendar calendar = Calendar.getInstance();
		    	calendar.set(Calendar.HOUR_OF_DAY, viewAlarmHour);
		    	calendar.set(Calendar.MINUTE, viewAlarmMinute);
		    	calendar.set(Calendar.SECOND, 00);
		    	
		    	Calendar current = Calendar.getInstance();
		        if(calendar.before(current)){
		      	  calendar.setTimeInMillis(calendar.getTimeInMillis()+(24*60*60*1000));
		        }
		      
		    	long interval = 24*60*60*1000;
		        if(settings.getBoolean("AlarmDebugModeOn", false))
		      	  interval=2*60*1000;
		    	
		    	alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), interval , pendingIntent);  //set repeating every 24 hours
		      		    
		    	Log.i("POWeR Tracker Special:", "Set View Alarm: "+calendar.get(Calendar.DATE)+" "+calendar.get(Calendar.HOUR_OF_DAY)+":"+calendar.get(Calendar.MINUTE));
			}
		
			if(updateAlarmHour!=-1&&updateAlarmMinute!=-1){			
				
				Intent myIntent = new Intent(PowerTrackerApplication.getContext() , DailyUpdateReminderNotification.class);     
			
		    	PendingIntent pendingIntent = PendingIntent.getService(PowerTrackerApplication.getContext(), 0, myIntent, 0);

		    	Calendar calendar = Calendar.getInstance();
		    	calendar.set(Calendar.HOUR_OF_DAY, updateAlarmHour);
		    	calendar.set(Calendar.MINUTE, updateAlarmMinute);
		    	calendar.set(Calendar.SECOND, 00);
		      
		    	Calendar current = Calendar.getInstance();
		        if(calendar.before(current)){
		      	  calendar.setTimeInMillis(calendar.getTimeInMillis()+(24*60*60*1000));
		        }
		    	
		    	long interval = 24*60*60*1000;
		        if(settings.getBoolean("AlarmDebugModeOn", false))
		      	  interval=2*60*1000;
		    	
		    	alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), interval , pendingIntent);  //set repeating every 24 hours
		      		    
		    	Log.i("POWeR Tracker Special:", "Set Update Alarm: "+calendar.get(Calendar.DATE)+" "+calendar.get(Calendar.HOUR_OF_DAY)+":"+calendar.get(Calendar.MINUTE));
				}
			
			if(questionnaireAlarmHour!=-1&&questionnaireAlarmMinute!=-1){			
				
				Intent myIntent = new Intent(PowerTrackerApplication.getContext() , DailyQuestionnaireReminderNotification.class);     
			
		    	PendingIntent pendingIntent = PendingIntent.getService(PowerTrackerApplication.getContext(), 0, myIntent, 0);

		    	Calendar calendar = Calendar.getInstance();
		    	calendar.set(Calendar.HOUR_OF_DAY, questionnaireAlarmHour);
		    	calendar.set(Calendar.MINUTE, questionnaireAlarmMinute);
		    	calendar.set(Calendar.SECOND, 00);
		      
		    	Calendar current = Calendar.getInstance();
		        if(calendar.before(current)){
		      	  calendar.setTimeInMillis(calendar.getTimeInMillis()+(24*60*60*1000));
		        }
		    	
		    	long interval = 24*60*60*1000;
		        if(settings.getBoolean("AlarmDebugModeOn", false))
		      	  interval=2*60*1000;
		    	
		    	alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), interval , pendingIntent);  //set repeating every 24 hours
		      		    
		    	Log.i("POWeR Tracker Special:", "Set Questionnaire Alarm: "+calendar.get(Calendar.DATE)+" "+calendar.get(Calendar.HOUR_OF_DAY)+":"+calendar.get(Calendar.MINUTE));
			}
			
		}
				
	}
	
	
	
}